<template>
<view class="container">
		<view class="header">
			<view class="subsection">
				<view :class=" index == subsectionCurrent?'subItem subItemCur':'subItem' " @click="subsectionChange(index)" v-for="(item, index) in subsectionList" :key="index">
					{{ item.name }}
				</view>
			</view>
			<view style="text-align: center;height: 90rpx;line-height: 90rpx;position: relative;">
				<text v-if="termPickerData.startTime" @click="termPickerData.show = true" style="font-size: 28rpx; background-color: #ffffff;padding: 10rpx 20rpx;border-radius: 40rpx;">
					{{ termPickerData.startTime }} 至 {{ termPickerData.endTime }}
				</text>
				<view   @click="popupWhere.show = true" style="position: absolute;right: 20rpx;z-index: 9px;top:0rpx;color: #1c6fc5;">
					筛选
				</view>
			</view>
		</view>
		<view class="wrapper" style="height: calc(100% - 200rpx);">
			<view class="main" >
				<scroll-view class="scroll" :scroll-y="true" :scroll-top="scrollTop" :show-scrollbar="false"
					:refresher-threshold="50" :refresher-triggered="refreshStatus" :lower-threshold="100"
					:scroll-with-animation="true" refresher-enabled="true" @refresherrefresh="onRefreshData"
					@scrolltolower="onLoadData" @scroll="onScroll">
					
					<view class="section" v-for="(item, index) in list" :key="index">
						<view class="card" @click="onLink('/pages/order/coursesBuy/detail/detail?id='+item.orderId)">
							<view class="cardHeader">
								<view class="ordernum">
									订单号：{{ item.orderId }}
								</view>
								<view class="orderstatus">
									{{ subsectionList.find(entity => entity.status == item.status).name }}
								</view>
							</view>
							<view class="content">
								<view class="pdleft">
									<image class="hostImage" :src="item.img[0]"></image>
								</view>
								<view class="pdright">
									<view class="desc">
										课程名称：{{ item.coursesName }}
									</view>
									<view  class="desc">
										开课时间：{{ item.effectiveDate + ' ' + item.effectiveTime}}
									</view>
									<view  class="desc">
										下单时间：{{ item.buyTime }}
									</view>
									<view class="price">
										<text class="rmb">￥</text><text class="amount">{{ item.orderAmt }}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
					
					<view class="nullData" v-if="page == 1 && list.length <=0">
						<view>
							<view>
								<image src="../../../static/image/notdata.png" class="nullImage"></image>
							</view>
							<view class="nullText">
								暂无记录
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		
		<!-- 日期控件 -->
		<term-picker
		  :visable.sync="termPickerData.show"
		  :defaultDate="termPickerData.defaultDate"
		  :minYear="1990"
		  :timeLimit="false"
		  :deferYear="0"
		  themeColor="#1c6fc5"
		  startText="开始时间"
		  endText="结束时间"
		  @confirm="calendarConfirm">
		</term-picker>
		<!-- 右侧弹出 -->
		<u-popup mode="right" :zIndex="100050 " :closeable="true" :show="popupWhere.show" @close="popupWhere.show = false" >
			<view style="width: 600rpx;">
				<view style="height: 100rpx;line-height: 100rpx;border-bottom: 1px solid #e5e5e5;text-align: center;font-size: 33rpx;">筛选</view>
				<view class="venueForm">
					<view style="color: #8f9ca2;padding-bottom: 15rpx;">
						订单号
					</view>
					<view class="form-row">
						<input class="fvalue" placeholder="输入订单号" v-model="queryParam.orderId" />
					</view>
					<view class="height30"></view>
					<view class="bordere5e5e5 "></view>
					<view class="height30"></view>
					<view style="color: #8f9ca2;padding-bottom: 15rpx;">
						课程名称
					</view>
					<view class="form-row">
						<input class="fvalue" placeholder="输入课程名称" v-model="queryParam.courseName" />
					</view>
					<view class="height30"></view>
					<view class="bordere5e5e5"></view>
					<view class="height30"></view>
					<view style="color: #8f9ca2;padding-bottom: 15rpx;">
						日期区间
					</view>
					<view style="display: flex;"  @click="termPickerData.show=true">
						<view class="form-row" style="width:45%">
							<view class="fvalue">{{ queryParam.startTime || '开始日期' }}</view>
						</view>
						<view class="timeCenter"> - </view>
						<view class="form-row" style="width:45%">
							<view class="fvalue">{{ queryParam.endTime || '结束日期' }}</view>
						</view>
					</view>
					<view style="padding: 20rpx 0rpx">
						<button @click="clearTime" class="clearTimeBtn">清除日期</button>
					</view>
					
					<view class="searchDiv">
						<button type="primary" @click="onSearchData">查询</button>
						<button style="margin-top: 20rpx;" @click="restForm">重置</button>
					</view>
				</view>
			</view>
		</u-popup>
		
	</view>
</template>

<script>
	import termPicker from '@/components/term-picker/term-picker.vue'
	export default {
		components:{
			termPicker
		},
		data() {
			return {
				//当前页数
				page: 1,
				//每页显示条数
				size: 10,
				//列表数据
				list: [],
				//总页数
				pages: 0,
				//监听滚动
				scrollTop: 0,
				realScrollTop: 0,
				refreshStatus: false,
				/**当前选择的状态序号**/
				subsectionCurrent: 0,
				/**状态分段**/
				subsectionList: [
					{status: '',name: '全部'},
					{status: '0',name: '待付款'},
					{status: '1',name: '已预约'},
					{status: '2',name: '已完成'},
					{status: '3',name: '已取消'},
					{status: '5',name: '退款中'}
				],
				//时间筛选控件
				termPickerData: {
					//显示控件
					show: false,
					//自定义数据
					defaultDate: [],
					//选择的开始时间
					startTime: '',
					//选择的结束时间
					endTime: ''
				},
				queryParam: {
					//订单编号
					orderId: '',
					//课程名称
					courseName: '',
					//选择的开始时间
					startTime: '',
					//选择的结束时间
					endTime: ''
				},
				popupWhere: {
					//显示
					show: false
				}
			}
		},
		onLoad() {
			this.fetchList();
		},
		methods: {
			// 获取记录
			fetchList() {
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				var params = {};
				params.current = this.page;
				params.size = this.size;
				var status = this.subsectionList[this.subsectionCurrent].status;
				params.orderStatus = status;
				//场馆编号过滤
				params.venueId = uni.getStorageSync('venueId');
				//时间段搜索
				if(this.queryParam.startTime){
					params.startDate = this.queryParam.startTime;
					params.endDate = this.queryParam.endTime;
				}
				//订单编号检索
				params.orderId = this.queryParam.orderId;
				params.courseName = this.queryParam.courseName;
				this.$request({
					url: '/blade-merchant-app/merchant-app/order/order-courses',
					data: params
				}).then(res => {
					const {
						records = [],
							pages
					} = res.data;
					
					records.forEach(item => {
						item.img = item.img.split(',');
					})
					
					if (this.page === 1) {
						this.list = records;
					} else {
						this.list = this.list.concat(records);
					}
					this.pages = pages || 0;
					setTimeout(() => {
						this.refreshStatus = false;
						uni.hideLoading();
					}, 300);
				})
			},
			// 监听滚动事件
			onScroll(e) {
				this.realScrollTop = e.detail.scrollTop;
			},
			// 刷新数据
			onRefreshData() {
				this.refreshStatus = true;
				this.page = 1;
				this.list = [];
				this.fetchList();
			},
			// 加载数据
			onLoadData() {
				if (this.page < this.pages) {
					this.page += 1;
					this.fetchList();
				}
			},
			//查询按钮事件
			onSearchData(){
				this.popupWhere.show = false;
				this.page = 1;
				this.list = [];
				this.fetchList(); 
			},
			//状态栏切换事件
			subsectionChange(index){
				this.subsectionCurrent = index;
				this.onRefreshData();
			},
			//选择时间段日期
			calendarConfirm(e){
				this.termPickerData.startTime=e[0];
				this.termPickerData.endTime=e[e.length-1];
				
				this.queryParam.startTime = e[0];
				this.queryParam.endTime = e[e.length-1];
				
				this.termPickerData.show = false;
				
			},
			//清空时间
			clearTime(){
				this.termPickerData.startTime= '';
				this.termPickerData.endTime = '';
				this.queryParam.startTime = '';
				this.queryParam.endTime = '';
			},
			//重置表单
			restForm(){
				this.clearTime();
				this.queryParam.orderId = '';
				this.queryParam.courseName = '';
			},
			onLink(url) {
				uni.navigateTo({ url: url })
			}
		}
	}
</script>



<style lang="scss">
	page {
		height: 100%;
		overflow: hidden;
		background: #f1f1f1;

		.container {
			height: 100%;
			
			.header {
				z-index: 1;
				height: 200rpx;
			
				.subsection {
					display: flex;
					background: #ffffff; 
					
					width: 100%;
					height: 60rpx;
					padding: 20rpx 0;
					
					.subItem {
						width: 20%;
						font-size: 33rpx;
						text-align: center;
					}
					
					.subItemCur {
						font-weight: 600;
						position: relative;
						padding-bottom:10rpx;
					}
					
					.subItemCur::after {
						position: absolute;
						content: ' ';
						width: 20%;
						height: 6rpx;
						bottom: 0px;
						background: red;
						left: 40%;
					}
					
				}
			}

			.wrapper {
				display: flex;
				flex-direction: column;
				height: 100%;

				.main {
					flex: 1;
					height: 100%;

					.scroll {
						height: 100%;

						.section {
							padding: 30rpx 30rpx 20rpx 30rpx;
							background: #ffffff;
							margin-bottom: 20rpx;
							

							.card {
								font-size: 30rpx;
								
								.cardHeader {
									display: flex;padding-bottom: 20rpx;
									
									.ordernum {
										width: 100%;
									}
									.orderstatus {
										width: 150rpx;text-align: right;
									}
								}
								
								.content {
									width: 100%;
									display: flex;
									
									.pdleft {
										padding-right:30rpx;
										
										.hostImage {
											width: 200rpx;
											height: 200rpx;
											border-radius: 10rpx;
										}
									}
									
									.pdright {
										
										.title {
											font-size: 35rpx;font-weight: 600;
										}
										.desc {
											font-size: 30rpx;color: #666666;line-height: 50rpx;
										}
										.price {
											color: red;
											.rmb {
												font-size: 30rpx;
											}
											.amount {
												font-size: 33rpx;
											}
										}
									}
									
									
								}
								
							}

							
						}
					}
				}
			}
		}
		
		
		.venueForm {
			padding: 30rpx;
			
			.form-row {
				border-radius: 8rpx;display: flex;border: 1px solid #c3c3c3;justify-content: center;align-items: center; 
				
				.ftitle {
					text-align: center;
					font-size: 30rpx;
					width: 180rpx;
					display: inline-table;
					color: #333333;
					padding:0rpx 10rpx;
				}
				.fvalue {
					line-height: 80rpx;
					height: 80rpx;width: 100%;font-size: 30rpx;padding: 0rpx 20rpx;width: 100%;
				}
			
			}
			
			.height30 {
				height: 30rpx;
			}
			
			.submitBtn {
				height: 80rpx;background-color: #0544ab;color: #ffffff;line-height: 90rpx;font-size: 32rpx;
			}
			
			.bordere5e5e5 {
				border:1px solid #e5e5e5;
			}
			
			.timeCenter {
				height: 38px;line-height: 38px;width: 10%;text-align: center;
			}
			
			.clearTimeBtn {
				height: 70rpx;line-height: 70rpx;font-size:30rpx
			}
			
			.searchDiv {
				padding: 100rpx; text-align: center;
			}
			
		}
		
		.nullData {
			height: 100%;
			display: flex;
			-webkit-box-pack: center;
			justify-content: center;
			-webkit-box-align: center;
			align-items: center;
			
			.nullImage {
				width: 120px;height: 120px;
			}
			.nullText {
				color:#666666;font-size:29rpx;padding-top:15rpx;text-align: center;
			}
			
		}
	}
	
</style>